<?php
/**
 * Copyright 2011  SURFfoundation
 * 
 * This file is part of ESCAPE.
 * 
 * ESCAPE is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * ESCAPE is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with ESCAPE.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * For more information:
 * http://escapesurf.wordpress.com/
 * http://www.surffoundation.nl/
 * 
 * Contact: d.vierkant@utwente.nl
 * 
 * @license http://www.gnu.org/licenses/gpl.html GNU GPLv3
 */
?>
<?php
/**
 * Remove ORE object property value
 * 
 * Remove a property value from an ORE object.
 */
class escape_repositoryui_rpc_RemoveOreObjectPropertyValue extends escape_repositoryui_RpcHandler
{
	public function handleRequest()
	{
		$subject = $_REQUEST['subject'];
		$predicate = $_REQUEST['predicate'];
		$object = $_REQUEST['object'];
		
		// fetch the object and aggregation
		$oreObject = $this->repository->getOreObjectByUri($subject);
		if($oreObject === null)
		{
			throw new escape_repositoryui_exception_Exception('Failed to fetch the specified object');
		}
		$aggregation = $oreObject->getAggregation();
		
		// only administrators, editors and owners are allowed to remove ore object property values
		$user = $this->repositoryUi->user;
		if($user === null || !($user->hasSystemRole(escape_repository_User::$role_administrator) || $aggregation->hasEditor($user->getUri()) || $aggregation->hasOwner($user->getUri())))
		{
			throw new escape_repositoryui_exception_Rpc("Not authorized");
		}

		// remove matching relation annotations
		$relationAnnotations = $this->repository->getRelationAnnotationsBySubjectPredicateObject($subject, $predicate, $object);
		foreach($relationAnnotations as $relationAnnotation)
		{
			$relationAnnotation->delete();
		}
		
		// remove statement
		$oreObjectProperties = $oreObject->getProperties();
		$oreObjectProperties->removeStatement($predicate, $object);
		$oreObjectProperties->removeStatement('http://purl.utwente.nl/ns/escape-system.owl#lastModifiedBy');
		$oreObjectProperties->addStatement('http://purl.utwente.nl/ns/escape-system.owl#lastModifiedBy', $user->getId());
		$oreObject->setProperties($oreObjectProperties);
	}
}
